
 <!DOCTYPE HTML>
<html>
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
  
    <title>Nginx优化及压力测试 | Zong&#39;s blog</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=3, minimum-scale=1">
    
    <meta name="author" content="Zong">
    
    <meta name="description" content="一、测试内容本次压力测试针对微校平台的nginx前端处理能力进行，评估nginx处理吞吐量
二、测试方法测试方法采用apache ab软件，在3台client发起对单一nginx服务的压测，模拟大并发html访问。每个client启动500并发，发起50000次请求。每次测试总共处理1500并发，1">
    
    
    
    
    
    <link rel="icon" href="/img/favicon.ico">
    
    
    <link rel="apple-touch-icon" href="/img/pacman.jpg">
    <link rel="apple-touch-icon-precomposed" href="/img/pacman.jpg">
    
    <link rel="stylesheet" href="/css/style.css">
</head>
</html>
  <body>
    <header>
      <div>
		
			<div id="imglogo">
				<a href="/"><img src="/img/logo.svg" alt="Zong&#39;s blog" title="Zong&#39;s blog"/></a>
			</div>
			
			<div id="textlogo">
				<h1 class="site-name"><a href="/" title="Zong&#39;s blog">Zong&#39;s blog</a></h1>
				<h2 class="blog-motto">日常积累，技术分享</h2>
			</div>
			<div class="navbar"><a class="navbutton navmobile" href="#" title="Menu">
			</a></div>
			<nav class="animated">
				<ul>
					<ul>
					 
						<li><a href="/">Home</a></li>
					
						<li><a href="/archives">Archives</a></li>
					
						<li><a href="/categories/运维">运维</a></li>
					
						<li><a href="/categories/容器架构">容器架构</a></li>
					
					<li>
					
					<form class="search" action="//baidu.com/s" method="get" accept-charset="utf-8">
						<label>Search</label>
						<input type="text" id="search" name="wd" autocomplete="off" maxlength="20" placeholder="Search" />
                        <input name=tn type=hidden value="bds">
                        <input name=cl type=hidden value="3">
                        <input name=ct type=hidden value="2097152">
						<input type="hidden" name="si" value="www.lstop.pub">
					</form>
					
					</li>
				</ul>
			</nav>			
</div>

    </header>
    <div id="container">
      <div id="main" class="post" itemscope itemprop="blogPost">
	<article itemprop="articleBody"> 
		<header class="article-info clearfix">
  <h1 itemprop="name">
    
      <a href="/2016/08/01/Nginx优化及压力测试/" title="Nginx优化及压力测试" itemprop="url">Nginx优化及压力测试</a>
  </h1>
  <p class="article-author">By
    
      <a href="http://www.lstop.pub" title="Zong">Zong</a>
    </p>
  <p class="article-time">
    <time datetime="2016-08-01T10:13:50.000Z" itemprop="datePublished">2016-08-01</time>
    
  </p>
</header>

	<div class="article-content">
		
		
		<div id="toc" class="toc-article">
			<strong class="toc-title">Contents</strong>
		<ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#一、测试内容"><span class="toc-number">1.</span> <span class="toc-text">一、测试内容</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#二、测试方法"><span class="toc-number">2.</span> <span class="toc-text">二、测试方法</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#三、测试环境"><span class="toc-number">3.</span> <span class="toc-text">三、测试环境</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#四、参数及名词解析"><span class="toc-number">4.</span> <span class="toc-text">四、参数及名词解析</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#五、测试数据"><span class="toc-number">5.</span> <span class="toc-text">五、测试数据</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#测试一"><span class="toc-number">5.1.</span> <span class="toc-text">测试一</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#测试二"><span class="toc-number">5.2.</span> <span class="toc-text">测试二</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#测试三"><span class="toc-number">5.3.</span> <span class="toc-text">测试三</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#测试四"><span class="toc-number">5.4.</span> <span class="toc-text">测试四</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#六、结论"><span class="toc-number">6.</span> <span class="toc-text">六、结论</span></a></li></ol>
		</div>
		
		<h1 id="一、测试内容"><a href="#一、测试内容" class="headerlink" title="一、测试内容"></a>一、测试内容</h1><p>本次压力测试针对微校平台的nginx前端处理能力进行，评估nginx处理吞吐量</p>
<h1 id="二、测试方法"><a href="#二、测试方法" class="headerlink" title="二、测试方法"></a>二、测试方法</h1><p>测试方法采用apache ab软件，在3台client发起对单一nginx服务的压测，模拟大并发html访问。<br>每个client启动500并发，发起50000次请求。每次测试总共处理1500并发，150000次请求。<br>ab命令：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ab -n 50000 -c 500 http://10.136.50.242/mmts/ab.html</span><br></pre></td></tr></table></figure>

<h1 id="三、测试环境"><a href="#三、测试环境" class="headerlink" title="三、测试环境"></a>三、测试环境</h1><p>所有测试服务器都是金山云虚拟主机<br>Nginx服务器：wx-dev-web，2核cpu，4g内存<br>Client服务器：wx-dev-test01，wx-dev-test02，wx-dev-officia，每服务器2核cpu，4g内存<br>网络带宽：千兆内网</p>
<h1 id="四、参数及名词解析"><a href="#四、参数及名词解析" class="headerlink" title="四、参数及名词解析"></a>四、参数及名词解析</h1><ol>
<li>压测结果名词</li>
</ol>
<blockquote>
<ul>
<li>Requests per second（QPS）：吞吐量，即每秒处理请求的数量。</li>
<li>Transfer rate：传输流量，单位KB/s，反映网络传输压力</li>
<li>统计均值：取每次测试样本的三个host的和除以样本数，取得的均值，公式：（样本1+样本2+样本3）/3</li>
</ul>
</blockquote>
<ol start="2">
<li>Nginx参数</li>
</ol>
<blockquote>
<ul>
<li>worker_processes：nginx 进程数，建议按照cpu 数目来指定。</li>
<li>worker_cpu_affinity：为每个进程分配cpu。</li>
<li>worker_rlimit_nofile：一个nginx 进程打开的最多文件描述符数目，使用系统参数ulimit -n指定也可以。</li>
<li>use epoll：使用epoll 的I/O 模型，Linux内核2.6版本及以后的系统推荐使用。</li>
<li>worker_connections：每个进程允许的最多连接数， 理论上每台nginx 服务器的最大连接数为worker_processes*worker_connections</li>
<li>accept_mutex：一个请求到达后，由所有worker争抢还是指定worker处理。</li>
<li>access_log off：关闭access log写入，能减少一点io等待压力。</li>
</ul>
</blockquote>
<ol start="3">
<li>OS内核参数</li>
</ol>
<blockquote>
<ul>
<li>net.ipv4.tcp_max_tw_buckets：参数用来设定timewait的数量，默认是180000，这里设为6000。</li>
<li>net.ipv4.ip_local_port_range：选项用来设定允许系统打开的端口范围。</li>
<li>net.ipv4.tcp_tw_recycle：选项用于设置启用timewait快速回收。</li>
<li>net.ipv4.tcp_tw_reuse：选项用于设置开启重用，允许将TIME-WAIT sockets重新用于新的TCP连接。</li>
<li>net.ipv4.tcp_syncookies：选项用于设置开启SYN Cookies，当出现SYN等待队列溢出时，启用cookies进行处理。</li>
<li>net.core.somaxconn：选项默认值是128， 这个参数用于调节系统同时发起的tcp连接数，在高并发的请求中，默认的值可能会导致链接超时或者重传，因此，需要结合并发请求数来调节此值。</li>
<li>net.core.netdev_max_backlog：选项表示当每个网络接口接收数据包的速率比内核处理这些包的速率快时，允许发送到队列的数据包的最大数目。</li>
<li>net.ipv4.tcp_max_orphans：选项用于设定系统中最多有多少个TCP套接字不被关联到任何一个用户文件句柄上。如果超过这个数字，孤立连接将立即被复位并打印出警告信息。这个限制只是为了防止简单的DoS攻击。不能过分依靠这个限制甚至人为减小这个值，更多的情况是增加这个值。</li>
<li>net.ipv4.tcp_max_syn_backlog：选项用于记录那些尚未收到客户端确认信息的连接请求的最大值。对于有128MB内存的系统而言，此参数的默认值是1024，对小内存的系统则是128。</li>
<li>net.ipv4.tcp_synack_retries：参数的值决定了内核放弃连接之前发送SYN+ACK包的数量。</li>
<li>net.ipv4.tcp_syn_retries：选项表示在内核放弃建立连接之前发送SYN包的数量。</li>
<li>net.ipv4.tcp_fin_timeou：t选项决定了套接字保持在FIN-WAIT-2状态的时间。默认值是60秒。正确设置这个值非常重要，有时候即使一个负载很小的Web服务器，也会出现因为大量的死套接字而产生内存溢出的风险。</li>
<li>net.ipv4.tcp_keepalive_time：选项表示当keepalive启用的时候，TCP发送keep</li>
</ul>
</blockquote>
<h1 id="五、测试数据"><a href="#五、测试数据" class="headerlink" title="五、测试数据"></a>五、测试数据</h1><h2 id="测试一"><a href="#测试一" class="headerlink" title="测试一"></a>测试一</h2><p>原始参数不做优化，只有业务配置，nginx的基本性能。从OS监控可以看到，资源消耗最大是CPU，其中sys比usr高，说明nginx进程对内核调用非常频繁。</p>
<table>
<thead>
<tr>
<th>样本</th>
<th>Requests per second</th>
<th>Transfer rate</th>
<th>更新</th>
<th>删除</th>
<th>95% Line</th>
<th>99% Line</th>
<th>Min</th>
<th>Max</th>
<th>Error %</th>
<th>Throughput</th>
<th>KB/sec</th>
</tr>
</thead>
<tbody><tr>
<td></td>
<td>host1</td>
<td>host2</td>
<td>host3</td>
<td>host1</td>
<td>host2</td>
<td>host3</td>
<td>0</td>
<td>2430</td>
<td>0.00%</td>
<td>81.1</td>
<td>11.6</td>
</tr>
<tr>
<td>1</td>
<td>5144</td>
<td>4998</td>
<td>5163</td>
<td>1214</td>
<td>1180</td>
<td>1220</td>
<td>0</td>
<td>2415</td>
<td>0.00%</td>
<td>81.1</td>
<td>.5</td>
</tr>
<tr>
<td>2</td>
<td>5086</td>
<td>4773</td>
<td>4794</td>
<td>1202</td>
<td>1128</td>
<td>1133</td>
<td>10</td>
<td>8164</td>
<td>0.00%</td>
<td>81.2</td>
<td>.0</td>
</tr>
<tr>
<td>3</td>
<td>5789</td>
<td>5245</td>
<td>5380</td>
<td>1368</td>
<td>1239</td>
<td>1271</td>
<td>3</td>
<td>4488</td>
<td>0.00%</td>
<td>81.1</td>
<td>.2</td>
</tr>
<tr>
<td>统计均值</td>
<td>15457</td>
<td>3652</td>
<td>510</td>
<td>3250</td>
<td>4163</td>
<td>5325</td>
<td>0</td>
<td>8164</td>
<td>0.00%</td>
<td>323.7</td>
<td>12.3</td>
</tr>
</tbody></table>
<h2 id="测试二"><a href="#测试二" class="headerlink" title="测试二"></a>测试二</h2><p>Nginx配置文件参数优化，包括基本优化work、connection、epoll、access_log off等。<br>从结果看和没优化也差不了多少，硬件资源是，特别CPU消耗是瓶颈，相同压力下比测试一的CPU消耗更大。</p>
<table>
<thead>
<tr>
<th>样本</th>
<th>Requests per second</th>
<th>Transfer rate</th>
<th>更新</th>
<th>删除</th>
<th>95% Line</th>
<th>99% Line</th>
<th>Min</th>
<th>Max</th>
<th>Error %</th>
<th>Throughput</th>
<th>KB/sec</th>
</tr>
</thead>
<tbody><tr>
<td></td>
<td>host1</td>
<td>host2</td>
<td>host3</td>
<td>host1</td>
<td>host2</td>
<td>host3</td>
<td>0</td>
<td>2430</td>
<td>0.00%</td>
<td>81.1</td>
<td>11.6</td>
</tr>
<tr>
<td>1</td>
<td>5968</td>
<td>6160</td>
<td>5971</td>
<td>1375</td>
<td>1419</td>
<td>1376</td>
<td>0</td>
<td>2415</td>
<td>0.00%</td>
<td>81.1</td>
<td>.5</td>
</tr>
<tr>
<td>2</td>
<td>6127</td>
<td>6086</td>
<td>6087</td>
<td>1412</td>
<td>1402</td>
<td>1402</td>
<td>10</td>
<td>8164</td>
<td>0.00%</td>
<td>81.2</td>
<td>.0</td>
</tr>
<tr>
<td>3</td>
<td>6073</td>
<td>6063</td>
<td>5991</td>
<td>1399</td>
<td>1397</td>
<td>1380</td>
<td>3</td>
<td>4488</td>
<td>0.00%</td>
<td>81.1</td>
<td>.2</td>
</tr>
<tr>
<td>统计均值</td>
<td>18175</td>
<td>4187</td>
<td>510</td>
<td>3250</td>
<td>4163</td>
<td>5325</td>
<td>0</td>
<td>8164</td>
<td>0.00%</td>
<td>323.7</td>
<td>12.3</td>
</tr>
</tbody></table>
<p>nginx参数文件：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br></pre></td><td class="code"><pre><span class="line">#user  nobody;</span><br><span class="line">worker_processes  2;</span><br><span class="line">#error_log  logs/error.log;</span><br><span class="line">error_log  logs/error.log  notice;</span><br><span class="line">#error_log  logs/error.log  info;</span><br><span class="line">#pid        logs/nginx.pid;</span><br><span class="line">events &#123;</span><br><span class="line">    use epoll;</span><br><span class="line">    worker_connections  65530;</span><br><span class="line">    accept_mutex off;</span><br><span class="line">&#125;</span><br><span class="line">http &#123;</span><br><span class="line">        include       /usr/sbin/nginx/conf/mime.types;</span><br><span class="line">        default_type  application/octet-stream;</span><br><span class="line">    #lua_package_path &apos;/usr/sbin/nginx/lua/?.lua;;/path/to/lua-resty-kafka/lib/?.lua;;/path/to/lua-resty-redis/lib/?.lua;;&apos;;</span><br><span class="line">    #access_log logs/access.log;</span><br><span class="line">    access_log off;</span><br><span class="line">    error_log off;</span><br><span class="line">        </span><br><span class="line">    #General Options</span><br><span class="line">    </span><br><span class="line">    server_names_hash_bucket_size 128;</span><br><span class="line">    client_header_buffer_size 64k;</span><br><span class="line">    large_client_header_buffers 4 128k;</span><br><span class="line">    server_tokens off;</span><br><span class="line">    ignore_invalid_headers   on;</span><br><span class="line">    recursive_error_pages    on;</span><br><span class="line">    server_name_in_redirect off; </span><br><span class="line">    #</span><br><span class="line">    sendfile        on;</span><br><span class="line">    </span><br><span class="line">    #TCP Options</span><br><span class="line">    tcp_nopush  on;</span><br><span class="line">    tcp_nodelay on;</span><br><span class="line">     </span><br><span class="line">    keepalive_timeout  65;</span><br><span class="line">    #size limits</span><br><span class="line">    gzip on;</span><br><span class="line">    gzip_min_length  1k;</span><br><span class="line">    gzip_buffers     4 16k;</span><br><span class="line">    gzip_http_version 1.0;</span><br><span class="line">    gzip_comp_level 2;</span><br><span class="line">    gzip_types       text/plain application/x-javascript text/css application/xml;</span><br><span class="line">    gzip_vary on;</span><br><span class="line">    #cache begin</span><br><span class="line">    proxy_buffering on;</span><br><span class="line">    proxy_cache_valid any 10m;</span><br><span class="line">    proxy_cache_path /data/nginxcache levels=1:2 keys_zone=ng-cache:8m max_size=1000m inactive=600m;</span><br><span class="line">    proxy_temp_path /data/nginxtemp;</span><br><span class="line">    proxy_buffer_size 4k;</span><br><span class="line">    proxy_buffers 100 8k;</span><br><span class="line">    open_file_cache max=65535 inactive=60s;</span><br><span class="line">    open_file_cache_valid 80s;</span><br><span class="line">    open_file_cache_min_uses 1;</span><br><span class="line">        server &#123;</span><br><span class="line">                listen       80;</span><br><span class="line">                server_name  localhost;</span><br><span class="line">                location / &#123;</span><br><span class="line">                    index  index.html index.htm;</span><br><span class="line">                    root  /data/nginx/html/;</span><br><span class="line">                    proxy_cache ng-cache;</span><br><span class="line">                    proxy_cache_valid 200 1h;</span><br><span class="line">                    #aio threads;</span><br><span class="line">                &#125;</span><br><span class="line">        &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>OS内核参数:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">net.ipv4.tcp_keepalive_time = 600</span><br><span class="line">net.ipv4.ip_local_port_range = 1024 65535</span><br><span class="line">net.ipv4.tcp_tw_reuse = 1</span><br><span class="line">net.ipv4.tcp_tw_recycle = 1</span><br><span class="line">net.ipv4.tcp_fin_timeout = 30</span><br><span class="line">net.ipv4.tcp_mem = 786432 1048576 1572864</span><br><span class="line">net.core.wmem_max = 873200</span><br><span class="line">net.core.rmem_max = 873200</span><br><span class="line">net.core.somaxconn = 262144 </span><br><span class="line">net.ipv4.tcp_wmem = 8192 436600 873200</span><br><span class="line">net.ipv4.tcp_rmem = 32768 436600 873200</span><br><span class="line">net.ipv4.tcp_max_syn_backlog = 2048</span><br><span class="line">net.ipv4.tcp_retries2 = 3</span><br><span class="line">net.ipv4.tcp_keepalive_intvl = 30</span><br><span class="line">net.ipv4.tcp_keepalive_probes = 3</span><br><span class="line">net.core.netdev_max_backlog = 262144</span><br></pre></td></tr></table></figure>

<h2 id="测试三"><a href="#测试三" class="headerlink" title="测试三"></a>测试三</h2><p>不使用apt的nginx，使用源码编译nginx，加入CPU，gcc等编译优化，增加Google tcmollac支持，nginx参数使用测试二参数。<br>测试使用的参数文件见测试二，Google tcmollac是一个内存分配器，管理堆内存，主要影响malloc和free，用于降低频繁分配、释放内存造成的性能损耗，并且有效地控制内存碎片。使用tcmollac代替glibc默认的内存管理能提升nginx的内存使用效率。<br>参考链接：<a href="http://blog.itpub.net/29510932/viewspace-1130106" target="_blank" rel="noopener">Nginx和TCMalloc</a><br>从结果看，QPS有约7%提升，同时CPU消耗稍稍下降。</p>
<table>
<thead>
<tr>
<th>样本</th>
<th>Requests per second</th>
<th>Transfer rate</th>
<th>更新</th>
<th>删除</th>
<th>95% Line</th>
<th>99% Line</th>
<th>Min</th>
<th>Max</th>
<th>Error %</th>
<th>Throughput</th>
<th>KB/sec</th>
</tr>
</thead>
<tbody><tr>
<td></td>
<td>host1</td>
<td>host2</td>
<td>host3</td>
<td>host1</td>
<td>host2</td>
<td>host3</td>
<td>0</td>
<td>2430</td>
<td>0.00%</td>
<td>81.1</td>
<td>11.6</td>
</tr>
<tr>
<td>1</td>
<td>6566</td>
<td>6576</td>
<td>6355</td>
<td>1513</td>
<td>1515</td>
<td>1464</td>
<td>0</td>
<td>2415</td>
<td>0.00%</td>
<td>81.1</td>
<td>.5</td>
</tr>
<tr>
<td>2</td>
<td>6849</td>
<td>6543</td>
<td>6534</td>
<td>1578</td>
<td>1508</td>
<td>1505</td>
<td>10</td>
<td>8164</td>
<td>0.00%</td>
<td>81.2</td>
<td>.0</td>
</tr>
<tr>
<td>3</td>
<td>6842</td>
<td>6515</td>
<td>6394</td>
<td>1576</td>
<td>1501</td>
<td>1473</td>
<td>3</td>
<td>4488</td>
<td>0.00%</td>
<td>81.1</td>
<td>.2</td>
</tr>
<tr>
<td>统计均值</td>
<td>19725</td>
<td>4544</td>
<td>510</td>
<td>3250</td>
<td>4163</td>
<td>5325</td>
<td>0</td>
<td>8164</td>
<td>0.00%</td>
<td>323.7</td>
<td>12.3</td>
</tr>
</tbody></table>
<p>另外对比测试了开启access log的情况，性能下降约10%，但是相信随着硬件提升，性能损失会更低。</p>
<table>
<thead>
<tr>
<th>样本</th>
<th>Requests per second</th>
<th>Transfer rate</th>
<th>更新</th>
<th>删除</th>
<th>95% Line</th>
<th>99% Line</th>
<th>Min</th>
<th>Max</th>
<th>Error %</th>
<th>Throughput</th>
<th>KB/sec</th>
</tr>
</thead>
<tbody><tr>
<td></td>
<td>host1</td>
<td>host2</td>
<td>host3</td>
<td>host1</td>
<td>host2</td>
<td>host3</td>
<td>0</td>
<td>2430</td>
<td>0.00%</td>
<td>81.1</td>
<td>11.6</td>
</tr>
<tr>
<td>1</td>
<td>6036</td>
<td>5834</td>
<td>5849</td>
<td>1391</td>
<td>1344</td>
<td>1348</td>
<td>0</td>
<td>2415</td>
<td>0.00%</td>
<td>81.1</td>
<td>.5</td>
</tr>
<tr>
<td>2</td>
<td>6254</td>
<td>5855</td>
<td>5854</td>
<td>1441</td>
<td>1349</td>
<td>1349</td>
<td>10</td>
<td>8164</td>
<td>0.00%</td>
<td>81.2</td>
<td>.0</td>
</tr>
<tr>
<td>3</td>
<td>6020</td>
<td>5924</td>
<td>5922</td>
<td>1387</td>
<td>1365</td>
<td>1364</td>
<td>3</td>
<td>4488</td>
<td>0.00%</td>
<td>81.1</td>
<td>.2</td>
</tr>
<tr>
<td>统计均值</td>
<td>17849</td>
<td>4113</td>
<td>510</td>
<td>3250</td>
<td>4163</td>
<td>5325</td>
<td>0</td>
<td>8164</td>
<td>0.00%</td>
<td>323.7</td>
<td>12.3</td>
</tr>
</tbody></table>
<h2 id="测试四"><a href="#测试四" class="headerlink" title="测试四"></a>测试四</h2><p>使用taobao tengine代替nginx，优化参数不变，从结果看QPS有约3%提升。除非对tengine独有的特性有需求，否则不需要特意替换成tengine。</p>
<table>
<thead>
<tr>
<th>样本</th>
<th>Requests per second</th>
<th>Transfer rate</th>
<th>更新</th>
<th>删除</th>
<th>95% Line</th>
<th>99% Line</th>
<th>Min</th>
<th>Max</th>
<th>Error %</th>
<th>Throughput</th>
<th>KB/sec</th>
</tr>
</thead>
<tbody><tr>
<td></td>
<td>host1</td>
<td>host2</td>
<td>host3</td>
<td>host1</td>
<td>host2</td>
<td>host3</td>
<td>0</td>
<td>2430</td>
<td>0.00%</td>
<td>81.1</td>
<td>11.6</td>
</tr>
<tr>
<td>1</td>
<td>6762</td>
<td>6635</td>
<td>6758</td>
<td>1571</td>
<td>1542</td>
<td>1570</td>
<td>0</td>
<td>2415</td>
<td>0.00%</td>
<td>81.1</td>
<td>.5</td>
</tr>
<tr>
<td>2</td>
<td>6637</td>
<td>6571</td>
<td>6918</td>
<td>1542</td>
<td>1527</td>
<td>1608</td>
<td>10</td>
<td>8164</td>
<td>0.00%</td>
<td>81.2</td>
<td>.0</td>
</tr>
<tr>
<td>3</td>
<td>6780</td>
<td>6697</td>
<td>6777</td>
<td>1575</td>
<td>1556</td>
<td>1575</td>
<td>3</td>
<td>4488</td>
<td>0.00%</td>
<td>81.1</td>
<td>.2</td>
</tr>
<tr>
<td>统计均值</td>
<td>20178</td>
<td>4689</td>
<td>510</td>
<td>3250</td>
<td>4163</td>
<td>5325</td>
<td>0</td>
<td>8164</td>
<td>0.00%</td>
<td>323.7</td>
<td>12.3</td>
</tr>
</tbody></table>
<h1 id="六、结论"><a href="#六、结论" class="headerlink" title="六、结论"></a>六、结论</h1><ul>
<li>nginx在低配置机器上即使不进行刻意优化也能提供稳定高效的http服务，当然进行优化会更好。</li>
<li>并发继续增大后nginx遇到的第一个瓶颈在CPU。</li>
<li>使用源码进行重新编译nginx可以使性能得到有效提升。</li>
<li>taobao tengine性能提升在此次测试环境没有特别明显，但是动态加载模块在运维中有实际意义。</li>
<li>有技术文档提到nginx1.9新版本支持SO_REUSEPORT 特性，性能比传统方式提高2倍，需要linux 3.9版本内核才支持，待测试。参考链接：<a href="http://www.oschina.net/translate/socket-sharding-nginx-release-1-9-1?print" target="_blank" rel="noopener">NGINX 1.9.1 中的 Socket 切分</a></li>
</ul>
  
	</div>
		<footer class="article-footer clearfix">

  <div class="article-tags">
  
  <span></span> <a href="/tags/压力测试/">压力测试</a><a href="/tags/nginx/">nginx</a>
  </div>


<div class="article-categories">
  <span></span>
  <a class="article-category-link" href="/categories/运维/">运维</a>
</div>



<div class="article-share" id="share">

  <div data-url="http://www.lstop.pub/2016/08/01/Nginx优化及压力测试/" data-title="Nginx优化及压力测试 | Zong&#39;s blog" data-tsina="" class="share clearfix">
  </div>

</div>
</footer>   	       
	</article>
	
<nav class="article-nav clearfix">
 
 <div class="prev" >
 <a href="/2016/08/10/网络运营商代码注入/" title="网络运营商代码注入">
  <strong>PREVIOUS:</strong><br/>
  <span>
  网络运营商代码注入</span>
</a>
</div>


<div class="next">
<a href="/2016/07/27/MongoDB-3-2-压力测试/"  title="MongoDB 3.2 压力测试">
 <strong>NEXT:</strong><br/> 
 <span>MongoDB 3.2 压力测试
</span>
</a>
</div>

</nav>

	
</div>  
      <div class="openaside"><a class="navbutton" href="#" title="Show Sidebar"></a></div>

  <div id="toc" class="toc-aside">
  <strong class="toc-title">Contents</strong>
  <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#一、测试内容"><span class="toc-number">1.</span> <span class="toc-text">一、测试内容</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#二、测试方法"><span class="toc-number">2.</span> <span class="toc-text">二、测试方法</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#三、测试环境"><span class="toc-number">3.</span> <span class="toc-text">三、测试环境</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#四、参数及名词解析"><span class="toc-number">4.</span> <span class="toc-text">四、参数及名词解析</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#五、测试数据"><span class="toc-number">5.</span> <span class="toc-text">五、测试数据</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#测试一"><span class="toc-number">5.1.</span> <span class="toc-text">测试一</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#测试二"><span class="toc-number">5.2.</span> <span class="toc-text">测试二</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#测试三"><span class="toc-number">5.3.</span> <span class="toc-text">测试三</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#测试四"><span class="toc-number">5.4.</span> <span class="toc-text">测试四</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#六、结论"><span class="toc-number">6.</span> <span class="toc-text">六、结论</span></a></li></ol>
  </div>

<div id="asidepart">
<div class="closeaside"><a class="closebutton" href="#" title="Hide Sidebar"></a></div>
<aside class="clearfix">

  
<div class="tagslist">
	<p class="asidetitle">Tags</p>
		<ul class="clearfix">
		
			<li><a href="/tags/Airtest/" title="Airtest">Airtest<sup>1</sup></a></li>
		
			<li><a href="/tags/DNS/" title="DNS">DNS<sup>1</sup></a></li>
		
			<li><a href="/tags/GitLab/" title="GitLab">GitLab<sup>1</sup></a></li>
		
			<li><a href="/tags/K8s/" title="K8s">K8s<sup>8</sup></a></li>
		
			<li><a href="/tags/Linux/" title="Linux">Linux<sup>1</sup></a></li>
		
			<li><a href="/tags/MongoDB/" title="MongoDB">MongoDB<sup>2</sup></a></li>
		
			<li><a href="/tags/OpenWrt/" title="OpenWrt">OpenWrt<sup>1</sup></a></li>
		
			<li><a href="/tags/Python/" title="Python">Python<sup>2</sup></a></li>
		
			<li><a href="/tags/RabbitMQ/" title="RabbitMQ">RabbitMQ<sup>1</sup></a></li>
		
			<li><a href="/tags/calico/" title="calico">calico<sup>1</sup></a></li>
		
			<li><a href="/tags/cdn/" title="cdn">cdn<sup>1</sup></a></li>
		
			<li><a href="/tags/docker/" title="docker">docker<sup>3</sup></a></li>
		
			<li><a href="/tags/docker-registry/" title="docker registry">docker registry<sup>1</sup></a></li>
		
			<li><a href="/tags/elasticsearch/" title="elasticsearch">elasticsearch<sup>3</sup></a></li>
		
			<li><a href="/tags/elk/" title="elk">elk<sup>3</sup></a></li>
		
			<li><a href="/tags/k8s/" title="k8s">k8s<sup>3</sup></a></li>
		
			<li><a href="/tags/kubernetes/" title="kubernetes">kubernetes<sup>1</sup></a></li>
		
			<li><a href="/tags/nginx/" title="nginx">nginx<sup>1</sup></a></li>
		
			<li><a href="/tags/python/" title="python">python<sup>1</sup></a></li>
		
			<li><a href="/tags/tomcat/" title="tomcat">tomcat<sup>1</sup></a></li>
		
		</ul>
</div>


  <div class="linkslist">
  <p class="asidetitle">Links</p>
    <ul>
      <li><a href="http://www.v2ex.com/?r=zong400" target="_blank" title="V2EX">V2EX</a></li>
      <li><a href="http://hexo.io" target="_blank" title="Hexo">Hexo</a></li>
	  <li><a href="https://promotion.aliyun.com/ntms/yunparter/invite.html?userCode=s0bh6uzq" target="_blank" title="阿里云">阿里云</a></li>
	  <li><a href="https://cloud.tencent.com/redirect.php?redirect=1014&cps_key=5bd9deb84d4d9f34b65fb934e12d03e3&from=console" target="_blank" title="腾讯云">腾讯云</a></li>
    </ul>
</div>


</aside>
</div>
    </div>
    <footer><div id="footer" >
	
	
	<div class="social-font" class="clearfix">
		
		
		
		
	</div>
		<p class="copyright">Hosted by <a href="https://pages.coding.me/" target="_blank" title="Coding Pages">Coding Pages</a></p>
		<p class="copyright">Powered by <a href="http://hexo.io" target="_blank" title="hexo">hexo</a> and Theme by <a href="https://github.com/wizicer/iceman" target="_blank" title="Iceman">Iceman</a> © 2020 
		
		<a href="http://www.lstop.pub" target="_blank" title="Zong">Zong</a>
		
		</p>
</div>
</footer>
    <script src="//cdn.staticfile.org/jquery/2.1.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){ 
  $('.navbar').click(function(){
    $('header nav').toggleClass('shownav');
  });
  var myWidth = 0;
  function getSize(){
    if( typeof( window.innerWidth ) == 'number' ) {
      myWidth = window.innerWidth;
    } else if( document.documentElement && document.documentElement.clientWidth) {
      myWidth = document.documentElement.clientWidth;
    };
  };
  var m = $('#main'),
      a = $('#asidepart'),
      c = $('.closeaside'),
      o = $('.openaside');
  $(window).resize(function(){
    getSize(); 
    if (myWidth >= 1024) {
      $('header nav').removeClass('shownav');
    }else
    {
      m.removeClass('moveMain');
      a.css('display', 'block').removeClass('fadeOut');
      o.css('display', 'none');
      
      $('#toc.toc-aside').css('display', 'none');
        
    }
  });
  c.click(function(){
    a.addClass('fadeOut').css('display', 'none');
    o.css('display', 'block').addClass('fadeIn');
    m.addClass('moveMain');
  });
  o.click(function(){
    o.css('display', 'none').removeClass('beforeFadeIn');
    a.css('display', 'block').removeClass('fadeOut').addClass('fadeIn');      
    m.removeClass('moveMain');
  });
  $(window).scroll(function(){
    o.css("top",Math.max(80,260-$(this).scrollTop()));
  });
});
</script>

<script type="text/javascript">
$(document).ready(function(){ 
  var ai = $('.article-content>iframe'),
      ae = $('.article-content>embed'),
      t  = $('#toc'),
      h  = $('article h2')
      ah = $('article h2'),
      ta = $('#toc.toc-aside'),
      o  = $('.openaside'),
      c  = $('.closeaside');
  if(ai.length>0){
    ai.wrap('<div class="video-container" />');
  };
  if(ae.length>0){
   ae.wrap('<div class="video-container" />');
  };
  if(ah.length==0){
    t.css('display','none');
  }else{
    c.click(function(){
      ta.css('display', 'block').addClass('fadeIn');
    });
    o.click(function(){
      ta.css('display', 'none');
    });
    $(window).scroll(function(){
      ta.css("top",Math.max(140,320-$(this).scrollTop()));
    });
  };
});
</script>


<script type="text/javascript">
$(document).ready(function(){ 
  var $this = $('.share'),
      url = $this.attr('data-url'),
      encodedUrl = encodeURIComponent(url),
      title = $this.attr('data-title'),
      tsina = $this.attr('data-tsina');
  var html = [
  '<a href="#" class="overlay" id="qrcode"></a>',
  '<div class="qrcode clearfix"><span>扫描二维码分享到微信朋友圈</span><a class="qrclose" href="#share"></a><strong>Loading...Please wait</strong><img id="qrcode-pic" data-src="http://s.jiathis.com/qrcode.php?url=' + encodedUrl + '"/></div>',
  '<a href="#textlogo" class="article-back-to-top" title="Top"></a>',
  '<a href="https://www.facebook.com/sharer.php?u=' + encodedUrl + '" class="article-share-facebook" target="_blank" title="Facebook"></a>',
  '<a href="#qrcode" class="article-share-qrcode" title="QRcode"></a>',
  '<a href="https://twitter.com/intent/tweet?url=' + encodedUrl + '" class="article-share-twitter" target="_blank" title="Twitter"></a>',
  '<a href="http://service.weibo.com/share/share.php?title='+title+'&url='+encodedUrl +'&ralateUid='+ tsina +'&searchPic=true&style=number' +'" class="article-share-weibo" target="_blank" title="Weibo"></a>',
  '<span title="Share to"></span>'
  ].join('');
  $this.append(html);
  $('.article-share-qrcode').click(function(){
    var imgSrc = $('#qrcode-pic').attr('data-src');
    $('#qrcode-pic').attr('src', imgSrc);
    $('#qrcode-pic').load(function(){
        $('.qrcode strong').text(' ');
    });
  });
});     
</script>









  </body>
</html>

